Skip to content

Icons: Add getAspectRatioIcon and use it in the media editor - #86348

Merged
mirka merged 7 commits into
WordPress:trunkfrom
00kaku:feat/aspect-ratio-icon-shared
Nov 11, 2026
Merged

mirka merged 7 commits into
WordPress:trunkfrom
00kaku:feat/aspect-ratio-icon-shared

Conversation

@00kaku

@00kaku 00kaku commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

What?

Follow-up to #85812 and #86344

Adds getAspectRatioIcon( ratio ) to @wordpress/icons: a function that returns an icon drawing the shape of an aspect ratio. The block editor's aspect ratio dropdown uses it instead of its local copy, and the media editor's crop panel dropdown now shows the same icons.

Why?

The aspect ratio icons from #85812 only existed inside the block editor. The media editor's dropdown, suggested by @t-hamano, needs them too, but @wordpress/media-editor is a bundled package and can't use private APIs. After the discussion in this PR, @mirka suggested @wordpress/icons, which is itself bundled, so the API stays safe to change or remove.

How?

  • @wordpress/icons: adds getAspectRatioIcon( ratio ), exported next to Icon and used as <Icon icon={ getAspectRatioIcon( 16 / 9 ) } />. The ratio is a number (width divided by height). A value that is not a positive number, such as 0, gets the generic aspectRatio icon.
  • The icon sets fill: none inline, as the other stroke icons do, so a parent rule like .components-button svg { fill: currentColor } does not fill the rectangle.
  • @wordpress/block-editor: the dimensions tool uses getAspectRatioIcon and its local copy is removed.
  • @wordpress/media-editor: the crop panel's aspect ratio dropdown shows the icons in the trigger and in the options. Free and Original use the generic icon, because drawing the image's own ratio would need its dimensions.
  • Storybook's icon list skips the new function, since it is not a static icon.

Testing Instructions

  1. Insert an Image block and select it. Open the Aspect ratio dropdown in the sidebar and confirm the icons look the same as on trunk.
  2. Enable the Media Editor experiment (Gutenberg > Experiments), open an image in the media editor and go to the Crop tab. Confirm the Aspect ratio dropdown shows an icon for each option, and the trigger shows the icon of the chosen ratio.

Automated: npm run test:unit -- packages/icons packages/media-editor/src/components/media-editor-crop-panel packages/block-editor/src/components/dimensions-tool

Use of AI Tools

I used Claude Code (Anthropic) to help investigate the issue, locate the cause in the code, and draft the fix. I reviewed the changes, ran the tests and verified the behavior in the browser myself, and I take responsibility for them.

… grid

Move the icon that draws the shape of an aspect ratio into @wordpress/components as a private component, and round the sides of the shapes to a half pixel so their outlines sit on the same grid as the generic aspect ratio icon.
@github-actions github-actions Bot added [Package] Components /packages/components [Package] Block editor /packages/block-editor labels Nov 5, 2026
@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground Preview in Storybook

🎉 Props

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: 00kaku <akashsrma@git.wordpress.org>
Co-authored-by: mirka <0mirka00@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: andrewserong <andrewserong@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

🏷️ Labels

This pull request needs exactly one label indicating its type, and has 0.

  • Required: any label starting with [Type].
  • Found: none.

Read more about Type labels in Gutenberg. If you cannot add labels yourself, a reviewer can do it for you.

@00kaku 00kaku changed the title Feat/aspect ratio icon shared Dimensions tool: Share the aspect ratio icon and snap it to the pixel grid Nov 5, 2026
…icon-shared

# Conflicts:
#	packages/block-editor/CHANGELOG.md
#	packages/block-editor/src/components/dimensions-tool/aspect-ratio-icon.tsx
#	packages/components/CHANGELOG.md

@mirka mirka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm actually wondering if @wordpress/icons could be the right home for this… Like a function that returns an SVG element: <Icon icon={ getAspectRatioIcon( 16 / 9 ) } />.

It would be the first non-static export in that package, so it would live next to Icon rather than in library/, and storybook/stories/icons/manifest.ts would need to skip it. What do you think @t-hamano?

@t-hamano

t-hamano commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

@00kaku Thanks for working on this!

@wordpress/media-editor is a bundled package, and the lint rule forbids it from using private APIs

Sorry, I missed this. It means that whether we put this dynamic icon in @wordpress/components or @wordpress/icons, the only way for the media editor to use it is to make it a public API.

My main question is whether @wordpress/media-editor is planned to be exposed on the wp global in the future. The package depends on WordPress features such as @wordpress/core-data and @wordpress/api-fetch, so it can't be used outside WordPress anyway, and I think it could be exposed on wp at some point. If it is, the dynamic aspect-ratio icon could stay private.

@ramonjd @andrewserong, are there any plans to expose the media editor on wp in the near future?

@andrewserong

Copy link
Copy Markdown
Contributor

My main question is whether @wordpress/media-editor is planned to be exposed on the wp global in the future.

@ramonjd can correct me here, but in the short-term I'd say it's unlikely to be exposed on the wp global until we look at stabilising the media editor route experiment, which is to say if/when we replace the core media editor with the new one. That's likely a long way off as we still have a bunch of unknowns (particular in regards to extensibility), but we're chipping away at building out features, so we are making progress 🙂

For the purposes here, I'd assume that wordpress/media-editor will remain bundled in the near-term. So, my main question for this PR would be: does this icon need to be private? What would the downside be of making it public?

(If it's a problem, personally I don't mind too much if the media editor's icon isn't up to date for a while)

@t-hamano

t-hamano commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

@andrewserong Thanks for the reply!

So, my main question for this PR would be: does this icon need to be private? What would the downside be of making it public?

Bundled packages are not allowed to import private APIs. So I think we have two options:

  • Keep the icon as an internal component of the block editor, as it is on trunk, and leave the media editor without it until the package is exposed on the wp global.
  • If the media editor needs the icon now, add it as a public API to either @wordpress/components or @wordpress/icons.

I don't have any concerns about making this dynamic icon public. I just wanted to keep it private if possible 🙂

@andrewserong

andrewserong commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

I don't have any concerns about making this dynamic icon public. I just wanted to keep it private if possible 🙂

I usually lean that way for new components, too, as once we make something public we're committed to it. In this case, though, the API surface is very small and seems unlikely to change anytime soon (a simple component with a ratio prop), so personally I wouldn't mind if it were made public in components. I'd lean on @mirka's take here though given Lena suggested starting out with private for this in the other PR: #85812 (comment)

Keep the icon as an internal component of the block editor, as it is on trunk, and leave the media editor without it until the package is exposed on the wp global.

That is also perfectly valid, though. I wouldn't consider this urgent for the media editor itself, and if y'all prefer to keep this private for now, we can add this as a to-do list item for the media editor so we remember to tidy this one when it's eventually stable.

@mirka

mirka commented Nov 9, 2026

Copy link
Copy Markdown
Member

I think we can go with @wordpress/icons then. The icon package is bundled, so we're safer in terms of future API changes and removal.

Move the aspect ratio icon from a private component in @wordpress/components
to a public function in @wordpress/icons, so the bundled media editor can use
it. The block editor and the media editor's crop panel use it, and the icon
sets fill: none inline so parent fill rules do not fill the shape.
@github-actions github-actions Bot added [Package] Icons /packages/icons and removed [Package] Components /packages/components labels Nov 10, 2026
@00kaku 00kaku changed the title Dimensions tool: Share the aspect ratio icon and snap it to the pixel grid Icons: Add getAspectRatioIcon and use it in the media editor Nov 10, 2026
@00kaku

00kaku commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

Thanks all, this is much clearer now! I've gone with @wordpress/icons as @mirka suggested: getAspectRatioIcon( ratio ) returns an SVG element and is exported next to Icon, and the media editor's crop panel now shows the icons in its dropdown. Storybook's icon list skips the new function.

I also applied the review fixes: fill: none is set inline, the weak test now checks that the generic icon is returned for 0, null and NaN, and the "free ratio" wording is gone from the docs. I merged trunk and re-tested the block editor and the media editor in the browser. Please take another look.

Comment thread docs/manifest.json Outdated
Comment on lines +1454 to +1459
{
"title": "AspectRatioIcon",
"slug": "aspect-ratio-icon",
"markdown_source": "../packages/components/src/aspect-ratio-icon/README.md",
"parent": "components"
},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is no longer needed. It should be removed once you run npm run docs:build.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed by running npm run docs:build, thanks! That should also fix the failing "Check local changes" CI step.

Comment on lines +77 to +90
it( 'draws an icon for each aspect ratio option', async () => {
const user = userEvent.setup();
setupCropPanel();

await user.click(
screen.getByRole( 'combobox', { name: 'Aspect ratio' } )
);

const square = await screen.findByRole( 'option', { name: 'Square' } );

// The icon has no accessible name, so look for the SVG itself.
// eslint-disable-next-line testing-library/no-node-access
expect( square.querySelector( 'svg' ) ).toBeInTheDocument();
} );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Select.Item always renders a check icon, and for unselected items it's simply hidden with opacity: 0. So this test would pass even without the aspect ratio icon.

I think we can just remove this test. The shape and the fallback to the generic icon are already covered by the unit tests in @wordpress/icons.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I've removed the test. The shape and the fallback to the generic icon are covered by the unit tests in @wordpress/icons.

Comment on lines +3 to +11
.media-editor-crop-panel__aspect-ratio-option {
display: flex;
align-items: center;
gap: $grid-unit-10;

svg {
flex-shrink: 0;
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the media editor is opened as a modal from the editor, it receives the aspect ratios defined in theme.json, so long labels can be passed in. So it might be better to add styles that account for truncation, as in the block editor's DimensionsTool implementation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the same truncation styles as the DimensionsTool. I checked in the media editor that a long label ends with an ellipsis in the trigger, and the icon keeps its size.

@mirka mirka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On narrow screens, aspect ratio is picked from the Menu in MediaEditorImageControls, and that menu still shows text only. Would it be worth adding the icons there too? (I like how our icon abstraction turned out to be handy for things like this too, instead of abstracting the full Select 😄)

Number( item.value )
) }
/>
{ item.label }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The trigger's ellipsis doesn't apply through the flex wrapper, so a long label is cut off without one. On trunk, the default trigger truncated it. The block editor's version handles this with a label span plus min-width: 0 on the wrapper. Can we do the same here, along with the matching __aspect-ratio-label rule in style.scss?

Suggested change
{ item.label }
<span className="media-editor-crop-panel__aspect-ratio-label">
{ item.label }
</span>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, the trigger label is now a span with the __aspect-ratio-label class, min-width: 0 is on the wrapper, and the matching rule is in style.scss, as in the block editor.

…narrow-screen menu

Wrap the trigger label in a span with min-width: 0 on the wrapper so a long
label gets an ellipsis, show the aspect ratio icons in the menu used on
narrow screens, remove a stale AspectRatioIcon entry from docs/manifest.json
and drop a test that passed without the icon.
@00kaku

00kaku commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

On narrow screens, aspect ratio is picked from the Menu in MediaEditorImageControls, and that menu still shows text only. Would it be worth adding the icons there too? (I like how our icon abstraction turned out to be handy for things like this too, instead of abstracting the full Select 😄)

@mirka Good idea, thanks! The narrow-screen menu now shows the aspect ratio icons too, using Menu.PrefixIcon with getAspectRatioIcon. Free and Original get the generic icon, as in the dropdown. Before and after at a narrow width:

Before After
image image

And agreed, the icon function was handy here.

@mirka mirka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@mirka
mirka merged commit a837e2d into WordPress:trunk Nov 11, 2026
101 checks passed
@github-actions github-actions Bot added this to the Gutenberg 24.5 milestone Nov 11, 2026
@paulopmt1 paulopmt1 added the [Type] Enhancement A suggestion for improvement. label Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Block editor /packages/block-editor [Package] Icons /packages/icons [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants